package design_patterns.command;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by nuno-rafael-figueiredo on 01-07-2014.
 */
public class CommandQueue extends Command{
    private final List<Command> queue = new ArrayList<>();

    @Override
    public void execute() {
        next();
    }

    private void next() {
        if (queue.isEmpty()){
            finish();
            return;
        }

        Command command = queue.remove(0);

        command.onFinish((a, b, c) -> next());
        command.execute();
    }

    public void add(Command command) {
        queue.add(command);
    }
}
